<template>
    <MyHead title="确认订单" @click-left="onClickLeft"></MyHead>
    <!-- 111111111111111111 -->
    <div class="top" v-for="item in Dz">
        <van-contact-card v-if="(item.isDefault)" type="edit" :tel="item.tel + ' (' + item.province + item.city + item.county
            + item.addressDetail + ')'" :name="item.name" @click="onAdd" />
    </div>
    <van-contact-card v-if="flag" type="add" @click="onEdit" />

    <!-- 2222222222222222222222 -->
    <div class="list">
        <div v-for="item in list">
            <van-card v-if="item.flag" :num="item.num" :price="item.originprice" :desc="item.proname" :title="item.proname"
                :thumb="item.img1" />
        </div>
    </div>
    <van-submit-bar :price="totalMoney * 100" button-text="确认订单" @submit="onSubmit" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { storeToRefs } from 'pinia'
import router from '@/router'

import { ress} from '../api/map'
import { CarList } from '../api/car'
import { addOrder, deleteCartItem,getOrderListApi } from '../api/order'

import { useCartStore } from '../store/cart'
import { useProductStore } from '../store/product'
import { onMounted } from 'vue';
const ProductStore = useProductStore()
const cartStore = useCartStore()
const { totalMoney } = storeToRefs(cartStore)

const onClickLeft = () => history.back();
const Dz = ref([])
const list = ref([])
const flag = ref(false)
const userid = localStorage.getItem('id')
onMounted(() => {
    ress(userid).then(result => {
        Dz.value = result.data

        for (var i = 0; i < Dz.value.length; i++) {
            if (Dz.value[i].isDefault == true) {
                return flag.value = false
            } else {
                flag.value = true
            }
        }
        return flag.value
    })

    CarList(userid).then(result => {
        console.log(result)
        list.value = result.data
        list.value = result.data.map((item, index) => {
            item.address = item.province + item.city + item.county + item.addressDetail;
            item.id = item.addressid;
            return item;
        })
    })


    // getOrderListApi(userid).then(res => {
    //     console.log(res);
    // })
})

//  添加地址
const onAdd = () => {
    router.push({ name: 'address' })
}

// 地址管理
const onEdit = () => {
    router.push({ name: 'address' })
}

const onSubmit = () => {
    addOrder(userid).then(res => {
        console.log(res.time)
        if (res.code == '200') {
            deleteCartItem(userid).then(res => {
                console.log(res)
                router.push('/')
            })
        }
    })

}
</script>

<style>
.list {
    margin-bottom: 50px;
}
</style>